package com.huang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;

@Controller
public class LoginController {
    @RequestMapping("/user/login")

    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        //model类用于储存一些需要传输的值
                        Model model,
                        //用于传输用户信息
                        HttpSession session){

        //业务逻辑
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            //在登录成功后，传递一个session值给拦截器
            session.setAttribute("loginUser",username);
            return "dashboard";
        }else {
            model.addAttribute("msg","用户名或者密码错误");
            return "index";
        }

    }

    @RequestMapping("/user/logout")
    public String loginout(HttpSession session){
        session.invalidate();
        return "redirect:/index.html";
    }
}
